import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import Far from './Far'

export default class App extends Component {
  componentDidMount() {
    // 订阅
    this.id = PubSub.subscribe('xxx', function (topic, data) {
      //topic还是话题, 没用
      // 传递过来的数据
      console.log('app', topic, data)
    })
    this.id1 = PubSub.subscribe('yyy', function (topic, data) {
      //topic还是话题, 没用
      // 传递过来的数据
      console.log('app', topic, data)
    })
  }

  componentWillUnmount() {
    PubSub.unsubscribe(this.id)
    PubSub.unsubscribe(this.id1)
  }
  render() {
    return (
      <div>
        <button
          onClick={() => {
            PubSub.unsubscribe(this.id)
          }}
        >
          清除app中关于xxx话题的指定订阅
        </button>
        <button
          onClick={() => {
            PubSub.unsubscribe('xxx')
          }}
        >
          清除所有xxx话题的订阅
        </button>
        <button
          onClick={() => {
            PubSub.clearAllSubscriptions()
          }}
        >
          清除当前项目中所有的订阅
        </button>
        ====================== App
        <hr />
        <Far></Far>
      </div>
    )
  }
}
